package com.sevenGroup.pawnshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sevenGroup.pawnshop.Utils.ParameterCalibrationUtil;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.BrandForm;
import com.sevenGroup.pawnshop.controller.form.SelectBrandForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.controller.info.BrandInfo;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Brand;
import com.sevenGroup.pawnshop.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/brand")
@Api(tags = "处理品牌相关的接口")
public class BrandController {

    @Resource
    private BrandService brandService;

    //新增品牌
    @PostMapping("saveBrand")
    @ApiOperation("新增品牌")
    @PreAuthorize("hasAnyAuthority('boss')")
    public Result saveBrand(@ApiParam("需要传入品牌对象") @Valid @RequestBody BrandForm brandForm, BindingResult bindingResult) throws sevenGroupException {
        System.out.println(1);
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        brandService.saveBrand(brandForm);
        return Result.success(null);
    }

    //查询所有品牌
    @PreAuthorize("hasAnyAuthority('boss','worker','shopManage')")
    @PostMapping("selectAllBrand")
    @ApiOperation("查询所有品牌")
    public Result selectAllBrand(@ApiParam("需要传入分页对象") @Valid @RequestBody pageForm pageForm, BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        IPage<BrandInfo> brandInfoIPage = brandService.selectAllBrand(pageForm);
        return Result.success(brandInfoIPage);
    }

    //根据状态查询所有品牌
    @PreAuthorize("hasAnyAuthority('boss','worker','shopManage')")
    @PostMapping("selectAllBrandByState")
    @ApiOperation("根据状态查询所有品牌")
    public Result selectAllBrandByState(){
        List<Brand> list = brandService.list(new QueryWrapper<Brand>().eq("brand_state", "显示"));
        return Result.success(list);
    }

    @ApiOperation("根据品牌id查询品牌信息")
    @PreAuthorize("hasAnyAuthority('boss')")
    @GetMapping("selectBrandByBrandId/{brandId}")
    public Result selectBrandByBrandId(@PathVariable Integer brandId) throws sevenGroupException {
        BrandInfo brandInfo = brandService.selectBrandByBrandId(brandId);
        return Result.success(brandInfo);
    }

    //根据id删除
    @PostMapping("delBrandById")
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("根据id删除")
    public Result delBrandById(@ApiParam("需要传入品牌id") Integer brandId) throws sevenGroupException {
        brandService.removeById(brandId);
        return Result.success(null);
    }

    //多选，根据集合删除
    @PostMapping("delBrandByList")
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("多选，根据集合删除")
    public Result delBrandByList(@ApiParam("需要传入品牌id集合") @RequestBody ArrayList<Integer> list) {
        brandService.removeByIds(list);
        return Result.success(null);
    }

    //更新品牌
    @PostMapping("updateBrand")
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("更新品牌")
    public Result updateBrand(@ApiParam("需要传入品牌对象") @Valid @RequestBody BrandForm brandForm, BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        brandService.updateBrand(brandForm);
        return Result.success(null);
    }

    //根据品牌名模糊查询
    @PostMapping("selectBrandByBrandName")
    @PreAuthorize("hasAnyAuthority('boss','worker','shopManage')")
    @ApiOperation("根据品牌名模糊查询")
    public Result selectBrandByBrandName(@ApiParam("需要传入查询条件的对象") @Valid @RequestBody SelectBrandForm selectBrandForm) throws sevenGroupException {
        IPage<BrandInfo> brandInfoIPage = brandService.selectBrandByBrandName(selectBrandForm);
        return Result.success(brandInfoIPage);
    }

    @ApiOperation("根据商品大类查询品牌集合")
    @PreAuthorize("hasAnyAuthority('boss','worker','shopManage')")
    @GetMapping("selectBrandByFirstTypeId/{firstTypeId}")
    public Result selectBrandByFirstTypeId(@PathVariable Integer firstTypeId) throws sevenGroupException {
        List<BrandInfo> brandInfos = brandService.selectBrandByFirstTypeId(firstTypeId);
        return Result.success(brandInfos);
    }

}

